package com.dhecp.project.system.service.impl;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.layui.TreeDataModel;
import com.dhecp.core.layui.TreeDataUtil;
import com.dhecp.core.layui.TreeSelectDataModel;
import com.dhecp.framework.util.ShiroUtils;

import com.dhecp.project.base.domain.BaseInfo;
import com.dhecp.project.base.service.BaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.dhecp.project.system.domain.SysDictionaryForModule;
import com.dhecp.project.system.domain.SysMenuInfo;
import com.dhecp.project.system.mapper.SysMenuInfoMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysMenuInfoService;

/**
 * 菜单 业务层处理
 * 
 * @author alula
 */
@Service("sysmenuinfo")
public class SysMenuInfoServiceImpl implements SysMenuInfoService {
    public static final String PREMISSION_STRING = "perms[\"{0}\"]";

    @Autowired
    private SysMenuInfoMapper menuInfoMapper;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

	@Autowired
	private TreeDataUtil treeDataUtil;
	@Autowired
	private BaseInfoService baseInfoService;


	/**
	 * 通过guid查询单条记录
	 * @param guid
	 * @return 实体SysMenuInfo
	 * @author lwl
	 */
    @Override
    public SysMenuInfo queryMenuInfoByGuid(String guid) {
        return menuInfoMapper.findMenuInfoByGuid(guid);
    }

    /**
	 * 查询菜单信息
	 * @param params
	 * @return List-SysMenuInfo
	 * @author lwl
	 * @throws Exception 
	 */
    @Override
    public List<SysMenuInfo> queryMenuInfo(Map<String, Object> params) throws Exception{
    	params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));		//常规查询条件SQL语句
        return menuInfoMapper.findMenuInfo(params);
    }
    
    /**
	 * 查询二级菜单信息
	 * @param params
	 * @return map
	 * @author lwl
	 * @throws Exception 
	 */
    @Override
	public Map<String, Object> queryNextMenu(Map<String, Object> params) throws Exception {
    	// List<SysMenuInfo> nextMenuList = menuInfoMapper.findNextMenu(params);
    	List<SysMenuInfo> nextMenuList = menuInfoMapper.findMenuInfo(params);//查询所有菜单
		Map<String, Object> nextMenuMap = new LinkedHashMap<String, Object>();
		for (SysMenuInfo nextMenu : nextMenuList) {
			nextMenuMap.put(nextMenu.getMenuCaption().toLowerCase(), TypeUtils.entityToMap(nextMenu));
		  } 
		return nextMenuMap;
	}

    /**
     * 获取当前用户拥有的功能和菜单树
     * @param params
     * @return List
     * @author lwl
     */
    @Override
    public List<TreeDataModel> queryMenuInfoTree(Map<String, Object> params) {
        params.put("loginUser", ShiroUtils.getUserId());
        params.put("mandator", ShiroUtils.getMandatorId());
        return menuInfoMapper.findMenuInfoTree(params);
    }

	/**
	 * 获取当前用户拥有的功能和菜单树/PC
	 * @param params
	 * @return List
	 * @author lwl
	 */
	@Override
	public List<TreeDataModel> queryMenuInfoTreePC(String moduleTable) {
		Map<String, Object> params = new HashMap<String, Object> ();
		params.put("loginUser", ShiroUtils.getUserId());
		params.put("mandator", ShiroUtils.getMandatorId());
		params.put("menuType", "ByModule");
//		return menuInfoMapper.findMenuInfoTree(params);
		List<TreeDataModel> treeDataModelList = new ArrayList<TreeDataModel>();
		List<BaseInfo> baseInfoList = new ArrayList<BaseInfo>();
		try{
			Map<String, Object> params2 = new HashMap<String, Object> ();
			String vtype = "modulegroup";
			params2.put("vtype", "modulegroup");
			moduleTable = "TB_baseinfo_" + vtype;
			moduleTable = (moduleTable.toLowerCase().indexOf("tb_baseinfo_area") == -1 ? moduleTable : "tb_baseinfo_area");
			params2.put("moduleTable", moduleTable);
			baseInfoList = baseInfoService.queryBaseInfoList(params2);
			treeDataModelList = menuInfoMapper.findMenuInfoTree(params);
//			treeDataModelList = treeDataUtil.dealTreeDataModel(treeDataModelList,treeDataModelList.get(0).getMaxlevels());
			treeDataModelList = treeDataUtil.dealTreeDataModelPc(treeDataModelList,treeDataModelList.get(0).getMaxlevels(),baseInfoList);

		}catch(Exception e){
			return treeDataModelList;
		}
		return treeDataModelList;
	}
	
	/**
	 * 获取菜单分组/PC
	 * @param params
	 * @return List
	 * @author lwl
	 */
	@Override
	public List<BaseInfo> menusGroup(String moduleTable) {
		Map<String, Object> params = new HashMap<String, Object> ();
		params.put("loginUser", ShiroUtils.getUserId());
		params.put("mandator", ShiroUtils.getMandatorId());
		params.put("menuType", "ByModule");
		List<BaseInfo> baseInfoList = new ArrayList<BaseInfo>();
		try{
			Map<String, Object> params2 = new HashMap<String, Object> ();
			String vtype = "modulegroup";
			params2.put("vtype", "modulegroup");
			moduleTable = "TB_baseinfo_" + vtype;
			moduleTable = (moduleTable.toLowerCase().indexOf("tb_baseinfo_area") == -1 ? moduleTable : "tb_baseinfo_area");
			params2.put("moduleTable", moduleTable);
			baseInfoList = baseInfoService.queryBaseInfoList(params2);

		}catch(Exception e){
			return baseInfoList;
		}
		return baseInfoList;
	}

	/**
	 * 获取当前用户拥有的功能和菜单树
	 * @param params
	 * @return List
	 * @author lwl
	 */
	@Override
	public List<TreeSelectDataModel> queryMenuInfoSelectTree(Map<String, Object> params) {
		params.put("loginUser", ShiroUtils.getUserId());
		params.put("mandator", ShiroUtils.getMandatorId());
		return menuInfoMapper.findMenuInfoSelectTree(params);
	}

    /**
     * 获取用户手机分组功能菜单
     * @param params
     * @return List - SysDictionaryForModule
     * @author DH-Xxj
     */
    @Override
    public List<SysDictionaryForModule> queryMobileMenuInfoList(Map<String, Object> params) {
        params.put("loginUser", ShiroUtils.getUserId());
        params.put("mandator", ShiroUtils.getMandatorId());
        return menuInfoMapper.findMobileMenuInfo(params);
    }
	
    /**
	 * 新增菜单信息
	 * @param menuInfoItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult insertMenu(Map<String, Object> params) {
		
		List<SysMenuInfo> menuInfoList = JSON.parseArray(params.get("menuInfoItems").toString(), SysMenuInfo.class);
        if (menuInfoList == null || menuInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (SysMenuInfo menuInfo : menuInfoList) {
//            if (menuInfoMapper.findMenuInfoByGuidCaption(menuInfo.getMenuCaption()) != null){
//                return AjaxResult.error("菜单已经存在，不能重复添加！");
//            }else{
//                menuInfoMapper.insertMenu(menuInfo);
//            }
            menuInfoMapper.insertMenu(menuInfo);
        }
        return AjaxResult.success("操作成功");
	}
	
	/**
	 * 修改菜单信息
	 * @param menuInfoItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
    public AjaxResult updateMenu(@RequestParam Map<String, Object> params) {
		List<SysMenuInfo> menuInfoList = JSON.parseArray(params.get("menuInfoItems").toString(), SysMenuInfo.class);
		if (menuInfoList == null || menuInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
		for (SysMenuInfo menuInfo : menuInfoList) {
			menuInfoMapper.updateMenu(menuInfo);
		}
		return AjaxResult.success("操作成功！");
    }
	
	/**
	 * 通过guid删除菜单信息
	 * @param menuInfoItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
    public AjaxResult deleteMenuByGuid(Map<String, Object> params) {
        
        List<SysMenuInfo> menuInfoList = JSON.parseArray(params.get("menuInfoItems").toString(), SysMenuInfo.class);
        if (menuInfoList == null || menuInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (SysMenuInfo menuInfo : menuInfoList) {
        	menuInfoMapper.deleteMenuByGuid(menuInfo);
        }
        return AjaxResult.success("操作成功");
    }

}
